﻿function CopyItemById(listId, itemId, siteUrl) {   
    var ajaxRequestUrl = siteUrl + "/_layouts/15/CopyItem/CopyItem.ashx?listId=" + listId + "&itemId=" + itemId;
    $.ajax(ajaxRequestUrl)
       .done(
           function (copyResult)
           {
               if (!copyResult.ErrorMessage) {
                   var editUrl = GetSiteUrl() + copyResult.UpdateFormUrl + "?ID=" + copyResult.NewItemId + "&source=" + document.location.href;
                   openDialogForMeetingRecord(editUrl, 'Update the copied item');
               }
               else {
                   console.error("Copy Item error:" + copyResult.ErrorMessage);
                   console.error("StackTrace:" + copyResult.ErrorDetail);
               }
           }
       )
       .fail(function (error) {
           errorCallBack(error);
       });
}
function openDialogForMeetingRecord(url, title) {
    var options = {
        url: url,
        title: title,
        dialogReturnValueCallback: CallBackfromDialog
    };
    SP.UI.ModalDialog.showModalDialog(options);
}

function CallBackfromDialog(dialogResult) {
    document.location.reload();
    /*if (dialogResult == 1) {
        // TBD：refresh the data        
    }
    else {
        // Do nothing
    }*/
}

function GetSiteUrl() {
    var port = "";
    if (document.location.port && document.location.port.lenght > 0) {
        port = ":" + document.location.port;
    }

    return document.location.protocol + '//' + document.location.host + port;
}